from django.db import models
from meiduo_mall.utils.models import BaseModel
# Create your models here.
from django.contrib.auth.models import AbstractUser
from areas.models import Area


class User(AbstractUser):
    mobile = models.CharField(max_length=11,unique=True,verbose_name='手机号')
    email_active = models.BooleanField(default=False,verbose_name='邮箱验证状态')
    # address.users  默认地址的用户对象　一对一随便写在一边
    default_address = models.ForeignKey('Address',on_delete=models.SET_NULL,null=True,blank=True,related_name='users',verbose_name='默认地址')
    class Meta:
        db_table = 'tb_users'
        verbose_name = '用户'
        verbose_name_plural = verbose_name


class Address(BaseModel):
    # user.user_address 用户的地址　多对一的模型对象关系写在多的那边
    user = models.ForeignKey(User,on_delete=models.CASCADE,related_name='user_address',verbose_name='用户')
    title = models.CharField(max_length=20,verbose_name='地址标题')
    receiver = models.CharField(max_length=20,verbose_name='收货人')
    province = models.ForeignKey('areas.Area',related_name='province',on_delete=models.PROTECT,null=True,verbose_name='省')
    city = models.ForeignKey('areas.Area',related_name='city',on_delete=models.PROTECT,null=True,verbose_name='市')
    district = models.ForeignKey('areas.Area',related_name='district',on_delete=models.PROTECT,null=True,verbose_name='区')
    place = models.CharField(max_length=50,verbose_name='地址')
    mobile = models.CharField(max_length=11,verbose_name='手机号')
    tel = models.CharField(max_length=20,null=True,blank=True,default='',verbose_name='固定电话')
    email = models.EmailField(max_length=30,null=True,blank=True,default='',verbose_name='电子邮箱')
    is_delete = models.BooleanField(default=False,verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_address'
        verbose_name = '用户地址'
        verbose_name_plural = verbose_name
        ordering = ['-update_time']